package com.control.qct.businesssteward.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.activity.OpenOrderActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.base.BaseFragment;
import com.control.qct.businesssteward.model.MemberInquireModel;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.utils.RegexUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 会员查询--侧滑菜单
 * Created by Administrator on 2017/11/28 0028.
 */

@SuppressLint("ValidFragment")
public class MemberInquireFragment extends BaseFragment {
    @BindView(R.id.et_member_inquire)
    EditText mEtMemberInquire;//会员查询
    @BindView(R.id.tv_member_inquire)
    TextView mTvMemberInquire;//查询
    @BindView(R.id.tv_member_code)
    TextView mTvMemberCode;//会员号
    @BindView(R.id.tv_member_name)
    TextView mTvMemberName;//会员名
    @BindView(R.id.tv_member_integral)
    TextView mTvMemberIntegral;//会员积分
    @BindView(R.id.tv_member_grade)
    TextView mTvMemberGrade;//会员等级
    @BindView(R.id.tv_member_status)
    TextView mTvMemberStatus;//会员状态
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;//取消
    @BindView(R.id.tv_sure)
    TextView mTvSure;//确定
    private OpenOrderActivity mOpenOrderActivity;
    private String mMemberPhone;
    private ICallbackMemberListener mICallbackMemberListener;
    private Bundle mBundle;
    private MemberInquireModel mMemberInquireModel;

    public MemberInquireFragment(ICallbackMemberListener listener) {
        this.mICallbackMemberListener = listener;
    }

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_member_inquire, container, false);
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mOpenOrderActivity = (OpenOrderActivity) context;
    }

    @OnClick({R.id.tv_member_inquire, R.id.tv_cancel, R.id.tv_sure})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_member_inquire://查询
                searchMemberHttp();
                break;
            case R.id.tv_cancel://取消
                mOpenOrderActivity.mDlMenu.closeDrawers();
                break;
            case R.id.tv_sure://确定
                if (mMemberInquireModel == null) {
                    showShortToast("请先查找会员！");
                    return;
                }
                mBundle = new Bundle();
                mBundle.putSerializable("member_information",mMemberInquireModel);
                mICallbackMemberListener.getMemberInformation(mBundle);
                break;
        }
    }

    /**
     * 联网查询会员
     */
    private void searchMemberHttp() {
        mMemberPhone = mEtMemberInquire.getText().toString().trim();
        if (TextUtils.isEmpty(mMemberPhone)) {
            showShortToast("请输入会员号或手机号码查询！");
            return;
        }
//        if (!RegexUtils.isMobileExact(mMemberPhone)) {
//            showShortToast("请输入正确的手机格式！");
//            return;
//        }
        mNetProtocol.searchMember(mOpenOrderActivity, mMemberPhone, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                mMemberInquireModel = (MemberInquireModel) baseBean.getData();
                mTvMemberCode.setText(mMemberInquireModel.getMemberNo());
                mTvMemberName.setText(mMemberInquireModel.getRealName());
                mTvMemberIntegral.setText(mMemberInquireModel.getUsableIntegral() + "");
                mTvMemberGrade.setText(mMemberInquireModel.getLevelTitle());
                mTvMemberStatus.setText(mMemberInquireModel.getGetStatusTitle());
                mEtMemberInquire.setText("");
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }

    /**
     * 回调返回会员信息
     */
    public interface ICallbackMemberListener {
        void getMemberInformation(Bundle bundle);
    }
}
